package com.itcast.service;


import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itcast.dao.PermissionMapper;
import com.itcast.health.pojo.PageResult;
import com.itcast.health.pojo.Permission;
import com.itcast.health.pojo.QueryPageBean;
import com.itcast.health.pojo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author CM
 * @version v1.0
 * @date 2019/12/2 14:07
 * @description TODO
 **/
@Service(interfaceClass = PermissionService.class)
@Transactional
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionMapper permissionDao;

    /**
     * 新增权限
     * @param permission
     */
    @Override
    public void add(Permission permission) {
        permissionDao.add(permission);
    }

    /**
     * 分页查询权限数据
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<Permission> pages =  permissionDao.findPage(queryPageBean.getQueryString());
        return new PageResult(pages.getTotal(),pages.getResult());

    }

    /**
     * 编辑权限数据
     * @param id
     * @return
     */
    @Override
    public Result edit(Integer id) {
        List<Permission> permission = permissionDao.findById(id);
        if (CollectionUtil.isNotEmpty(permission)) {
            return new Result(true, "", permission.get(0));
        } else {
            return new Result(false, "");
        }
    }

    /**
     * 删除权限数据
     * @param id
     * @return
     */
    @Override
    public Result delete(Integer id) {
        try {
            permissionDao.deleteById(id);
           return new Result(true, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除失败");
        }
    }

    /**
     * 更新权限数据
     * @param permission
     */
    @Override
    public void update(Permission permission) {
        permissionDao.update(permission);
    }
}
